package com.example.auth.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.auth.user.entity.SystemUser;
import com.example.auth.user.service.SystemUserService;
import com.example.system.jwt.TokenUtil;
import com.example.system.tools.ResultHelper;
import com.example.system.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author HL
 */
@Controller
@RequestMapping("/")
public class LoginController {

    @Autowired
    SystemUserService service;

    /**
     * 登录页面
     * @return
     */
    @RequestMapping("/login")
    public String login(){
        return "page/login-1";
    }


    /**
     * 登录方法
     */
    @RequestMapping("/loginByUserInfo")
    @ResponseBody
    public ResultHelper login(SystemUser user){
        QueryWrapper<SystemUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",user.getUserName());
        SystemUser systemUser = service.getOne(queryWrapper);
        if(systemUser == null){
            return ResultHelper.fail("没有该用户！");
        }
        String password = MD5.getMD5(user.getUserPassword());
        if(!systemUser.getUserPassword().equals(password)){
            return ResultHelper.fail("密码错误！");
        }
        String token = TokenUtil.getToken(systemUser);
        return ResultHelper.success(token);
    }
}
